package zzu.mxd.answer.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import zzu.mxd.answer.entity.Recording;

import zzu.mxd.answer.service.IRecordingService;
import zzu.mxd.answer.vo.RecordingVO;

import zzu.mxd.common.PageRequest;
import zzu.mxd.common.R;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 答题记录表 前端控制器
 * </p>
 *
 * @author mxd
 * @since 2018-11-08
 */
@Api(tags = "答题记录管理")
@RestController
@RequestMapping("/answer/recording")
public class RecordingController {

    @Autowired
    private IRecordingService recordingService;

    /**
     * 学生ID、科目ID、题目ID
     * @param recording
     * @param pageRequest
     * @return
     */
    @ApiOperation("条件查询答题记录")
    @GetMapping("/selectPage")
    public R<IPage<Recording>> selectPage(Recording recording, PageRequest pageRequest){
        return R.succC(
                recordingService.page(
                new Page<>(pageRequest.getCurrent(),pageRequest.getSize()),
                new QueryWrapper<>(recording))
        );
    }

    /**
     *
     * @param studentId
     * @param subjectId
     * @return
     */
    @ApiOperation("查询某个学生单科正确率")
    @GetMapping("/selectRate")
    public R<Double> selectRate(String studentId,Long subjectId){
        List<RecordingVO> recordings = recordingService.selectBySubjectId(subjectId,studentId);
        int correctNum = 0;
        for (int i = 0;i<recordings.size();i++) {
            if (recordings.get(i).calculateRight()==1){
                correctNum++;
            }
        }
        float f1 = new BigDecimal( (float)correctNum/recordings.size()).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        return R.succC(Double.valueOf(f1));
    }
    /**
     *
     * @param subjectId 科目ID
     * @param studentId 学生ID
     * @return
     */
    @ApiOperation("查询某个学生单科答题总量")
    @GetMapping("/selectTotal")
    public R<Integer> selectTotal(long subjectId, String studentId){
        Integer recordings = recordingService.selectCount(subjectId,studentId);
        return R.succC(recordings);
    }


}
